package com.wxzz.elearing.file.util;


import com.wxzz.elearing.file.service.impl.ObsService;
import com.wxzz.elearning.common.vo.ReturnVO;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.time.LocalDate;
import java.util.Random;
import java.util.UUID;


public class ImageUtil {
    static final String IMAGE_SUFFIX = ".png";

    /**
     * @return 合成图片地址
     */
    public static String drawImage(String posterImgUrl, String tempQrCodeImgUrl, String credentialCode, String itemText, String nameText, String starText,
                                   String credentialTextOne, String credentialTextTwo, String credentialTextThree, String cqText, String createTime, String endTime, String enterpriseName, String commonUrlOne, String commonUrlTwo) throws IOException {

        String imgUrl = "";
        int width = 2657;
        int height = 1772;

        BufferedImage bgBufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// RGB形式
        Graphics2D bgBufImageGraphics = bgBufImage.createGraphics();
        bgBufImageGraphics.setBackground(Color.WHITE);// 设置背景色
        bgBufImageGraphics.clearRect(0, 0, width, height);// 通过使用当前绘图表面的背景色进行填充来清除指定的矩形。

        // bgBufImageGraphics.setBackground(new Color(255,255,255));
        bgBufImageGraphics.setPaint(Color.black);// 设置画笔,设置Paint属性
        Font font = new Font("宋体", Font.PLAIN, 48);
        bgBufImageGraphics.setFont(font);
        // 抗锯齿
        bgBufImageGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 计算文字长度，计算居中的x点坐标
        // int widthX = (width - textWidth) / 2;
        int cqCodeWidthX = 272;
        // 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
        bgBufImageGraphics.drawString(cqText, cqCodeWidthX, 958);

        // BufferedImage posterBufImage = ImageIO.read(new URL(posterImgUrl));
        // //直接使用图片做背景，自定义背景使用上面方式
        // Graphics2D posterBufImageGraphics = posterBufImage.createGraphics();

        BufferedImage posterBufImage = ImageIO.read(new URL(posterImgUrl));
        //BufferedImage qrCodeImage = ImageIO.read(new URL(tempQrCodeImgUrl));//二维码
        //BufferedImage commonOneImage = ImageIO.read(new URL(commonUrlOne));
        BufferedImage commonTwoImage = ImageIO.read(new URL(commonUrlTwo));

        // 设置圆形头像
//        BufferedImage roundHeadImg = new BufferedImage(commonOneImage.getWidth(), commonOneImage.getHeight(),
//                BufferedImage.TYPE_INT_RGB);

//        Graphics2D roundHeadGraphics = roundHeadImg.createGraphics();
//        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, roundHeadImg.getWidth(), roundHeadImg.getHeight());
//        roundHeadGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//        roundHeadImg = roundHeadGraphics.getDeviceConfiguration().createCompatibleImage(commonOneImage.getWidth(),
//                commonOneImage.getHeight(), Transparency.TRANSLUCENT);
//        roundHeadGraphics = roundHeadImg.createGraphics();
        // 使用 setRenderingHint 设置抗锯齿
//        roundHeadGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//        roundHeadGraphics.setClip(shape);
//        //roundHeadGraphics.drawImage(commonOneImage, 0, 0, null);
//        roundHeadGraphics.dispose();

        // bgBufImageGraphics.drawImage(qrCodeImage, (posterBufImage.getWidth()
        // - qrCodeImage.getWidth()), 10, qrCodeImage.getWidth(),
        // qrCodeImage.getHeight(), null);
        // posterBufImageGraphics.drawImage(roundHeadImg, 50, 100,
        // HEAD_URL_WIDTH, HEAD_URL_HEIGHT, null);
        bgBufImageGraphics.drawImage(posterBufImage, 0, 0, 2657, 1772, null);
        //bgBufImageGraphics.drawImage(roundHeadImg, 1500, 1200, 398, 398, null);
        bgBufImageGraphics.drawImage(commonTwoImage, 2000, 1200, 398, 398, null);
        //bgBufImageGraphics.drawImage(qrCodeImage, 13, 650, 150, 150, null);二维码
        // 计算文字长度，计算居中的x点坐标
        FontMetrics fm = bgBufImageGraphics.getFontMetrics(font);
        int textWidth = fm.stringWidth(nameText);
        int centerWidthX = (width - textWidth) / 2;
        int widthX = 270;
        bgBufImageGraphics.setColor(Color.black);
        // 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
        bgBufImageGraphics.drawString(credentialCode, 1800, 300);
        bgBufImageGraphics.drawString(itemText, widthX, 600);
        bgBufImageGraphics.drawString(nameText, widthX, 700);
        bgBufImageGraphics.drawString(starText, widthX, 800);
        bgBufImageGraphics.drawString(credentialTextOne, widthX, 900);
        bgBufImageGraphics.drawString(credentialTextTwo, centerWidthX, 1000);
        bgBufImageGraphics.drawString(credentialTextThree, widthX, 1100);
        bgBufImageGraphics.drawString(createTime, 1900, 1300);
        bgBufImageGraphics.drawString(endTime, 1900, 1400);
        bgBufImageGraphics.drawString(enterpriseName, 1900, 1500);
        bgBufImageGraphics.dispose();

        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ImageOutputStream imgOut = ImageIO.createImageOutputStream(bs);
        ImageIO.write(bgBufImage, "png", imgOut);
        // 上传到服务器上
        // InputStream inSteam = new ByteArrayInputStream(bs.toByteArray());
        // String url = OSSFactory.build().uploadSuffix(inSteam, IMAGE_SUFFIX);
        // 上传到服务器上
        //String url = OssUtil.uploadObject2OSS(bs.toByteArray());

        InputStream inputStream = null;
        OutputStream outputStream = null;
        inputStream = new ByteArrayInputStream(bs.toByteArray());

        ObsService obsService = new ObsService();
        imgUrl = obsService.upload(inputStream);
        String uuid = UUID.randomUUID().toString();
        File file = new File("./"+uuid+".jpg");
        if (!file.exists()) {
            file.createNewFile();
        }
        outputStream = new FileOutputStream(file);
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len = inputStream.read(buf, 0, 1024)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.flush();
        if (file.exists()) {
            file.delete();
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        return imgUrl;// 返回合成的图片地址url
    }

    public static void main(String[] args) throws IOException {
        //个人星级证书 https://wxzz.obs.cn-south-1.myhuaweicloud.com:443/%E4%B8%AA%E4%BA%BA.1psd.jpg
        //班组星级证书
        //工厂体检证书 https://wxzz.obs.cn-south-1.myhuaweicloud.com:443/%E5%B7%A5%E5%8E%82.1psd.jpg
        //公章 https://wxzz.obs.cn-south-1.myhuaweicloud.com:443/%E7%BB%84+1.png
        String haibao = "https://changchuan-crm.oss-cn-hangzhou.aliyuncs.com/2020crm/1596535815373.png";
        // 底图
        String backgroundImageUrl = "https://wxzz.obs.cn-south-1.myhuaweicloud.com:443/%E4%B8%AA%E4%BA%BA.1psd.jpg";
        // String backgroundImageUrl =
        // "http://img1.juimg.com/171010/330841-1G01000050879.jpg";
        // 二维码
        // String qrCodeImageUrl =
        // "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553504957130&di=02fae20a5c0f885d52299b2b1d682c86&imgtype=0&src=http%3A%2F%2Fimg.atobo.com%2FProductImg%2FEWM%2FUWeb%2F3%2F2%2F1%2F3%2F061%2F3213061%2F1.gif";
        //String qrCodeImageUrl = "http://xiaofang-exam.oss-cn-hangzhou.aliyuncs.com/userImg/1592466618481.png";
        // 公章
        String commonUrl = "https://wxzz.obs.cn-south-1.myhuaweicloud.com:443/%E7%BB%84+1.png";
        // String headUrl =
        // "http://pic.51yuansu.com/pic3/cover/00/63/25/589bdedf5475d_610.jpg";
        String credentialCode = "证书编号:1401489128012410882";
        String itemText = "项目名称:星级认证测评1";
        String nameText = "获得人名称:赵鹏飞";
        String starText = "等级:五星";
        String credentialTextOne = "技术要求:精通springboot、springcloud、rocketmq、neo4j、zookeeper、double";
        String credentialTextTwo = "《星级认证测评1指南》";
        String credentialTextThree = "上述项目符合:啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊";
        String createTime = "发证日期:" + LocalDate.now().toString();
        String endTime = "有效期至:" + LocalDate.now().toString();
        String enterpriseName = "精工智造有限公司";
        String cqText = "";
        String url = ImageUtil.drawImage(backgroundImageUrl, null, credentialCode, itemText, nameText, starText,
                credentialTextOne, credentialTextTwo, credentialTextThree, cqText, createTime, endTime, enterpriseName, commonUrl, commonUrl);
        System.out.println(url);

    }
}
